/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.jgls.bi.dataprocess;

import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultReader;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.sql.Date;
import java.util.Iterator;

/**
 *
 * @author Administrator
 */
public class LotteryMxJdbcDao extends JdbcDaoSupport implements LotteryMxDataDao {

    public void LotteryMxData(Date adTsnDate, int aiShopId, String asShopBm,
            int aiDateNo, String asYear, int aiYybh, int aiMan_id, String asTname) {
        int iSaleAmt = 0, iEncashAmt = 0;
        int iRc = 0;
        int aiYear = 0;
        String sql = "";
        float fSaleValue = 0, fEncashValue = 0;
        List lstLottery;
        System.out.println("now processing    LOTTERY's Data... Date:" + adTsnDate +
                " shopid;" + aiShopId + " please wait...");
        aiYear = Integer.parseInt(asYear);
        //取销售金额
        JdbcTemplate jt = getJdbcTemplate();
        sql = "select sum(sl) as saleamt,sum(je) as salevalue  from " + asTname +
                " where saledate = ? and bm=? and trnno<>9999 and substr(lb,1,4)='0829' ";
        Object[] params = new Object[]{adTsnDate, asShopBm};
        int[] types = {Types.DATE, Types.VARCHAR};
        lstLottery = jt.query(sql, params, types, new RowMapperResultReader(new LotteryMxRM()));
        Iterator Iter = lstLottery.iterator();
        LotteryMx ltam = new LotteryMx();
        while (Iter.hasNext()) {
            ltam = (LotteryMx) Iter.next();
            iSaleAmt = ltam.getSale_amt();
            fSaleValue = ltam.getSale_value();

        }
        //取兑奖金额
        sql = "select cpsl as encashamt,djje as encashvalue  from sy_lottery_dj_new " +
                " where djsj = ? and lsdid=?  ";
        params = new Object[]{adTsnDate, aiShopId};
        types = new int[]{Types.DATE, Types.INTEGER};
        lstLottery = jt.query(sql, params, types, new RowMapperResultReader(new LotteryMxRM2()));
        Iter = lstLottery.iterator();
        ltam = new LotteryMx();
        while (Iter.hasNext()) {
            ltam = (LotteryMx) Iter.next();
            iEncashAmt = ltam.getEncash_amt();
            fEncashValue = ltam.getEncash_value();

        }

        //begin insert into lotteryMx
        //1:delete
        params = new Object[]{adTsnDate, aiShopId};
        types = new int[]{Types.DATE, Types.INTEGER};
        sql = "delete from t_bi_lottery_mx where tsn_date = ? and tsn_id = ?";
        iRc = jt.update(sql, params, types);
        //2:insert
        sql = "insert into t_bi_lottery_mx " +
                "(tsn_date,tsn_id,yybh,man_id,sale_amt,sale_value,encash_amt" +
                ",encash_value)" +
                " values(?,?,?,?,?,?,?,?)";
        params = new Object[]{adTsnDate, aiShopId, aiYybh, aiMan_id,
            iSaleAmt, fSaleValue, iEncashAmt, fEncashValue
        };
        types = new int[]{Types.DATE, Types.INTEGER,
            Types.INTEGER, Types.INTEGER, Types.INTEGER,
            Types.FLOAT, Types.INTEGER, Types.FLOAT
        };
        iRc = jt.update(sql, params, types);
    }

    class LotteryMxRM implements RowMapper {

        public Object mapRow(ResultSet rs, int index)
                throws SQLException {
            LotteryMx lmx = new LotteryMx();
            lmx.setSale_value(rs.getFloat("salevalue"));
            lmx.setSale_amt(rs.getInt("saleamt"));
            return lmx;
        }
    }

    class LotteryMxRM2 implements RowMapper {

        public Object mapRow(ResultSet rs, int index)
                throws SQLException {
            LotteryMx lmx = new LotteryMx();
            lmx.setEncash_amt(rs.getInt("encashamt"));
            lmx.setEncash_value(rs.getFloat("encashvalue"));
            return lmx;
        }
    }
}
